# -*- coding: utf-8 -*-
# @Time    :2022/1/21 20:37
# @AUTHOR  :MORAN
# @Email   :moran@maqu.com
# @File    :tasks.py
# @Software:PyCharm


import logging

from django.core.mail import send_mail

from promote.settings import EMAIL_HOST_USER


def send_mail_task(email, username, password, classes_name):
    """
    发送邮件通知
    :param email:  收件人
    :param username:  用户名
    :param password:  初始密码
    :param classes_name:  班级名
    :return:
    """
    email_param = {
        'subject': '码趣教育-PROMOTE系统',
        'message': '',
        'html_message':
            f"""
                <h2>欢迎加入码趣教育[{classes_name}]</h2>
                <h3>诚邀您使用码趣教育-PROMOTE系统</h3>
                <p>其中包含: 知识点习题，升班考试测试，技术论坛等学习相关模块</p>
                <p>您的登录名为: <span style='color:aqua'>{username}</span></p>
                <p>您的初始密码为:<span>{password}</span> 建议您尽快修改密码</p>
                <p><a href='#'>点击访问码趣教育-PROMOTE系统</a></p>
            """,
        'from_email': EMAIL_HOST_USER,
        'recipient_list': [email],
    }
    try:
        res_email = send_mail(**email_param)
    except Exception as e:
        logging.error(f'邮件验证码发送[异常][email: {email},message: {e}]')
    else:
        if res_email != 1:
            logging.warning(f'邮件验证码发送[失败][email: {email}]')
        else:
            logging.info(f'邮件验证码发送[成功][email:{email},username:{username}]')

